package com.huabo.system.service.impl;

import com.hbfk.util.JedisUtil;
import com.hbfk.util.JwtUtils;
import com.hbfk.util.PageInfo;
import com.huabo.system.config.DateBaseConfig;
import com.huabo.system.mysql.entity.TblBiReportMenuMySql;
import com.huabo.system.mysql.mapper.TblBiReportMenuMySqlMapper;
import com.huabo.system.oracle.entity.TblBiReportMenu;
import com.huabo.system.oracle.entity.TblStaff;
import com.huabo.system.oracle.mapper.TblBiReportMenuMapper;
import com.huabo.system.service.TblBiReportMenuService;
import io.jsonwebtoken.Claims;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TblBiReportMenuServiceImpl implements TblBiReportMenuService {


    @Resource
    private TblBiReportMenuMapper tblBiReportMenuMapper;


    @Resource
    private TblBiReportMenuMySqlMapper tblBiReportMenuMySqlMapper;


    @Override
    public TblBiReportMenu geTblBiReportMenu(String id) {
        return tblBiReportMenuMapper.selectId(id);
    }

    @Override
    public TblBiReportMenuMySql geMySqlTblBiReportMenu(String id) {
        return tblBiReportMenuMySqlMapper.selectId(id);
    }

    @Override
    public Object isExistBiPageCode(String code) {

        Integer num = 0;
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {

            num = tblBiReportMenuMapper.selectCode(code);
        } else {

            num = tblBiReportMenuMySqlMapper.selectCode(code);
        }


        return num > 0 ? false : true;
    }

    @Override
    public void updateReportMenu(TblBiReportMenu page) {
        tblBiReportMenuMapper.updateReportMenu(page);
    }

    @Override
    public void updateMySqlReportMenu(TblBiReportMenuMySql page) {
        tblBiReportMenuMySqlMapper.updateReportMenu(page);
    }

    @Override
    public void saveReportMenu(TblBiReportMenu tbrm) {
        tblBiReportMenuMapper.insertTbrm(tbrm);
    }

    @Override
    public void saveMySqlReportMenu(TblBiReportMenuMySql tbrm) {
        tblBiReportMenuMySqlMapper.insertTbrm(tbrm);
    }


    @Override
    public void deleteReportMenu(TblBiReportMenu geTblBiReportMenu) {
        tblBiReportMenuMapper.delete(geTblBiReportMenu);
    }

    @Override
    public void deleteMySqlReportMenu(TblBiReportMenuMySql geTblBiReportMenu) {
        tblBiReportMenuMySqlMapper.delete(geTblBiReportMenu);
    }

    @Override
    public TblBiReportMenu selectTblBiReportMenu(String idStr) {
            return tblBiReportMenuMapper.selectTblBiReportMenu(idStr);
    }

    @Override
    public TblBiReportMenuMySql selectMySqlTblBiReportMenu(String idStr) {
        return tblBiReportMenuMySqlMapper.selectTblBiReportMenu(idStr);
    }

    @Override
    public void addPage(TblBiReportMenu page) {
        tblBiReportMenuMapper.addPage(page);
    }

    @Override
    public void addMySqlPage(TblBiReportMenuMySql page) {
        tblBiReportMenuMySqlMapper.addPage(page);
    }

    @Override
    public List<TblBiReportMenu> selectByFatherid(String idStr) {
            return tblBiReportMenuMapper.selectIdStr(idStr);
    }

    @Override
    public List<TblBiReportMenuMySql> selectByMySqlFatherid(String idStr) {
        return tblBiReportMenuMySqlMapper.selectIdStr(idStr);
    }

    @Override
    public Map<String, Object> finreportMenuList(Integer pageNumber, Integer pageSize, String token, String staffId, String type) {


        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        Jedis jedis = null;
        if (token == null && staffId == null) {
            resultMap.put("code", "0");
            resultMap.put("msg", "用户已失效！");
            return resultMap;
        }
        if (token == null && staffId != null) {
            jedis = JedisUtil.getJedis();
            token = jedis.get(staffId);
        }


        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            PageInfo<TblBiReportMenu> pageInfo = new PageInfo<TblBiReportMenu>();
            Claims claims = JwtUtils.parseJwt(token);
            Object object = claims.get("staffInfo");
            JSONObject objJson = JSONObject.fromObject(object);
            TblStaff staff = (TblStaff) JSONObject.toBean(objJson, TblStaff.class);
            BigDecimal orgid = staff.getCurrentOrg().getOrgid();

            pageInfo.setCurrentPage(pageNumber);
            pageInfo.setPageSize(pageSize);
            pageInfo.setTlist(tblBiReportMenuMapper.selectMenuList(pageInfo, orgid, type));
            pageInfo.setTotalRecord(tblBiReportMenuMapper.selectMenuCount());
            resultMap.put("data", pageInfo);

        } else {


            PageInfo<TblBiReportMenuMySql> pageInfo = new PageInfo<TblBiReportMenuMySql>();
            Claims claims = JwtUtils.parseJwt(token);
            Object object = claims.get("staffInfo");
            JSONObject objJson = JSONObject.fromObject(object);
            TblStaff staff = (TblStaff) JSONObject.toBean(objJson, TblStaff.class);
            BigDecimal orgid = staff.getCurrentOrg().getOrgid();

            pageInfo.setCurrentPage(pageNumber);
            pageInfo.setPageSize(pageSize);
            pageInfo.setTlist(tblBiReportMenuMySqlMapper.selectMenuList(pageInfo, orgid, type));
            pageInfo.setTotalRecord(tblBiReportMenuMySqlMapper.selectMenuCount());
            resultMap.put("data", pageInfo);

        }


        resultMap.put("code", "1");
        resultMap.put("msg", "访问接口成功");

        return resultMap;

    }

    @Override
    public TblBiReportMenu geTblBiReport(BigDecimal pageid) {
            return tblBiReportMenuMapper.geTblBiReport(pageid);
    }

    @Override
    public TblBiReportMenuMySql geMySqlTblBiReport(BigDecimal pageid) {
        return tblBiReportMenuMySqlMapper.geTblBiReport(pageid);
    }

    @Override
    public void deleteReportMenuPageId(BigDecimal pageid) {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            tblBiReportMenuMapper.deletePageId(pageid);
        } else {
            tblBiReportMenuMySqlMapper.deletePageId(pageid);
        }

    }
}
